package com.bluefox.hospital.controller;

import com.bluefox.common.web.dto.result.ListResultDTO;
import com.bluefox.common.web.dto.result.PageResultDTO;
import com.bluefox.common.web.dto.result.ResultDTO;
import com.bluefox.hospital.convertor.DoctorConvertor;
import com.bluefox.hospital.dto.DoctorDTO;
import com.bluefox.hospital.model.Doctor;
import com.bluefox.hospital.repository.DoctorRepository;
import com.bluefox.hospital.service.DoctorService;
import com.bluefox.hospital.utils.UploadAndDeleteFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Doctor的管理接口
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/w/doctors")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private DoctorRepository doctorRepository;
    @Autowired
    private DoctorConvertor doctorConvertor;

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @RequestMapping(value = "/s", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResultDTO<DoctorDTO> search(@RequestParam(value = "departmentId", required = false) Long departmentId, final Pageable pageable) {
        final Page<Doctor> models = this.doctorService.findByDepartmentId(departmentId,pageable);
        return this.doctorConvertor.toResultDTO(models);
    }

    /**
     * 取得详细数据
     *
     * @param id 资源ID
     * @return 资源详细
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<DoctorDTO> get(@PathVariable final Long id) {
        final Doctor model = this.doctorService.get(id);
        return this.doctorConvertor.toResultDTO(model);
    }

    /**
     * 新建操作
     *
     * @param doctorDTO 新建资源的DTO
     * @return 新建资源
     */

    @RequestMapping(method = RequestMethod.POST)
    public ResultDTO<DoctorDTO> create(@RequestBody @Valid final DoctorDTO doctorDTO) {
        final Doctor model = this.doctorConvertor.toModel(doctorDTO);
        this.doctorService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Doctor.class.getSimpleName(), model.getId());
        }
        return this.doctorConvertor.toResultDTO(model);
    }

    /**
     * 更新操作
     *
     * @param id        更新资源的ID
     * @param doctorDTO 更新资源的DTO
     * @return 更新后资源
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<DoctorDTO> update(@PathVariable final Long id, @RequestBody @Valid final DoctorDTO doctorDTO) {
        doctorDTO.setId(id);
        final Doctor model = this.doctorConvertor.toModel(doctorDTO);
        this.doctorService.update(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", Doctor.class.getSimpleName(), model.getId());
        }
        return this.doctorConvertor.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param Id 资源ID
     * @return 操作结果
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.doctorService.delete(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was deleted.", Doctor.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }

    @RequestMapping(value = "/findByDepartmentId", method = RequestMethod.POST)
    public ListResultDTO<DoctorDTO> findByDepartmentId(@RequestParam(value = "departmentId", required = false) Long departmentId, final Pageable pageable) {
        final Page<Doctor> byDepartmentId = this.doctorService.findByDepartmentId(departmentId, pageable);
        return this.doctorConvertor.toResultDTO(byDepartmentId);
    }
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public List<Map<String, Object>> upload(HttpServletRequest servletRequest ){
        return UploadAndDeleteFile.upload(servletRequest, "D:\\upload\\standard");
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public List<DoctorDTO> findAll(){
        List<Doctor> models = this.doctorRepository.findAll();
        return this.doctorConvertor.toListDTO(models);
    }
}
